package org.javaforever.gatescore.reports;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontDropdown;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.StatementList;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.reports.verb.ListActiveOption;
import org.javaforever.gatescore.reports.verb.ListActiveSumOption;
import org.javaforever.gatescore.reports.verb.Setup;
import org.javaforever.gatescore.reports.verb.Translate;
import org.javaforever.gatescore.utils.StringUtil;
import org.javaforever.gatescore.verb.ListActive;
import org.javaforever.gatescore.widgets.Widget;

public class EchartsDiagram extends Widget{
	protected FrontDomain domain;
	protected List<FrontField> xAxisFields = new ArrayList<>();
	protected FrontField yName;
	protected String detailPrefix = "";
	protected Set<FrontDomain> tranlateDomains = new TreeSet<>();
	protected Set<FrontDomain> parentTranlateDomains = new TreeSet<>();
	protected ListActiveOption listOption;
	protected ListActiveSumOption listSumOption;
	protected Setup setup;
	
	@Override
	public StatementList generateWidgetStatements() {
		List<Writeable> sList = new ArrayList<>();		
		sList.add(new Statement(2000L,0,"<el-select v-model=\"chartType\" placeholder=\"请选择\" @change=\"setup\">"));
		sList.add(new Statement(3000L,1,"<el-option"));
		sList.add(new Statement(4000L,2,"v-for=\"item in chartTypes\""));
		sList.add(new Statement(5000L,2,":key=\"item.value\""));
		sList.add(new Statement(6000L,2,":label=\"item.label\""));
		sList.add(new Statement(7000L,2,":value=\"item.value\"/>"));
		sList.add(new Statement(9000L,0,"</el-select>"));
		sList.add(new Statement(11000L,0,"<el-select v-model=\"dataType\" placeholder=\"请选择\" @change=\"setup\">"));
		sList.add(new Statement(12000L,1,"<el-option"));
		sList.add(new Statement(13000L,2,"v-for=\"item in dataTypes\""));
		sList.add(new Statement(14000L,2,":key=\"item.value\""));
		sList.add(new Statement(15000L,2,":label=\"item.label\""));
		sList.add(new Statement(16000L,2,":value=\"item.value\"/>"));
		sList.add(new Statement(18000L,0,"</el-select>"));
		sList.add(new Statement(20000L,0,"<div id=\"main\" ref=\"chart\" :style=\"{width: '1000px', height: '600px'}\"/>"));

		StatementList rsl = WriteableUtil.merge(sList);
		rsl.setSerial(this.serial);
		return rsl;
	}

	@Override
	public StatementList generateWidgetScriptStatements() throws Exception {
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"import {"));
		ListActive la = new ListActive(this.domain);
		sList.add(new Statement(2000L,1,StringUtil.lowerFirst(la.getVerbName())));
		sList.add(new Statement(3000L,0,"} from '@/api/"+this.domain.getPlural().toLowerCase()+"'"));
		if (this.yName instanceof FrontDropdown) {
			FrontDomain target = ((FrontDropdown)this.yName).getTarget();
			ListActive tla = new ListActive(target);
			sList.add(new Statement(4000L,0,"import {"));
			sList.add(new Statement(5000L,1,StringUtil.lowerFirst(tla.getVerbName())));
			sList.add(new Statement(7000L,0,"} from '@/api/"+target.getPlural().toLowerCase()+"'"));
		}
		sList.add(new Statement(8000L,0,""));
		sList.add(new Statement(9000L,0,"export function isBlank(val) {"));
		sList.add(new Statement(10000L,1,"if (val === undefined || val === null || val === '') return true"));
		sList.add(new Statement(11000L,1,"else return false"));
		sList.add(new Statement(12000L,0,"}"));
		sList.add(new Statement(13000L,0,""));
		sList.add(new Statement(14000L,0,"export default {"));
		sList.add(new Statement(15000L,1,"name: 'Echart',"));
		sList.add(new Statement(16000L,1,"data() {"));
		sList.add(new Statement(17000L,2,"return {"));
		sList.add(new Statement(19000L,3,"option: {},"));
		if (this.yName instanceof FrontDropdown) {
			FrontDomain target = ((FrontDropdown)this.yName).getTarget();
			sList.add(new Statement(20000L,3,"active"+StringUtil.capFirst(target.getPlural())+": [],"));
		}
		sList.add(new Statement(21000L,3,"active"+StringUtil.capFirst(this.domain.getPlural())+": [],"));
		sList.add(new Statement(22000L,3,"chartTypes: [{"));
		if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
			sList.add(new Statement(23000L,4,"value: 'line',"));
			sList.add(new Statement(24000L,4,"label: 'Line'"));
			sList.add(new Statement(25000L,3,"}, {"));
			sList.add(new Statement(26000L,4,"value: 'bar',"));
			sList.add(new Statement(27000L,4,"label: 'Bar'"));
			sList.add(new Statement(28000L,3,"}, {"));
			sList.add(new Statement(29000L,4,"value: 'pie',"));
			sList.add(new Statement(30000L,4,"label: 'Pie'"));
			sList.add(new Statement(31000L,3,"}],"));
			sList.add(new Statement(32000L,3,"chartType: 'line',"));
			sList.add(new Statement(33000L,3,"dataTypes: [{"));
			sList.add(new Statement(34000L,4,"value: 'original',"));
			sList.add(new Statement(35000L,4,"label: 'Original'"));
			sList.add(new Statement(36000L,3,"}, {"));
			sList.add(new Statement(37000L,4,"value: 'sum',"));
			sList.add(new Statement(38000L,4,"label: 'Sum'"));
			sList.add(new Statement(39000L,3,"}],"));
			sList.add(new Statement(40000L,3,"dataType: 'original',"));
			sList.add(new Statement(41000L,2,"}"));
		} else {
			sList.add(new Statement(23000L,4,"value: 'line',"));
			sList.add(new Statement(24000L,4,"label: '折线图'"));
			sList.add(new Statement(25000L,3,"}, {"));
			sList.add(new Statement(26000L,4,"value: 'bar',"));
			sList.add(new Statement(27000L,4,"label: '柱状图'"));
			sList.add(new Statement(28000L,3,"}, {"));
			sList.add(new Statement(29000L,4,"value: 'pie',"));
			sList.add(new Statement(30000L,4,"label: '饼图'"));
			sList.add(new Statement(31000L,3,"}],"));
			sList.add(new Statement(32000L,3,"chartType: 'line',"));
			sList.add(new Statement(33000L,3,"dataTypes: [{"));
			sList.add(new Statement(34000L,4,"value: 'original',"));
			sList.add(new Statement(35000L,4,"label: '原始数据'"));
			sList.add(new Statement(36000L,3,"}, {"));
			sList.add(new Statement(37000L,4,"value: 'sum',"));
			sList.add(new Statement(38000L,4,"label: '累加数据'"));
			sList.add(new Statement(39000L,3,"}],"));
			sList.add(new Statement(40000L,3,"dataType: 'original',"));
			sList.add(new Statement(41000L,2,"}"));
		}
		sList.add(new Statement(42000L,1,"},"));
		sList.add(new Statement(43000L,1,"created() {"));
		sList.add(new Statement(45000L,1,"},"));
		sList.add(new Statement(46000L,1,"mounted() {"));
		sList.add(new Statement(47000L,2,"this.setup()"));
		sList.add(new Statement(48000L,1,"},"));
		sList.add(new Statement(49000L,1,"methods: {"));
		StatementList sl = this.setup.generateControllerMethod().getMethodStatementList();
		sl.setSerial(50000L);
		sl.setIndent(2);
		sList.add(sl);
	
		if (this.yName instanceof FrontDropdown) {
			FrontDomain target = ((FrontDropdown)this.yName).getTarget();
			ListActive tla = new ListActive(target);
			StatementList tsl = tla.generateControllerMethod().getMethodStatementList();
			tsl.setSerial(66000L);
			sList.add(tsl);
		}
		StatementList osl = la.generateControllerMethod().getMethodStatementList();
		osl.setSerial(72000L);
		sList.add(osl);
		
		if (this.yName instanceof FrontDropdown) {
			FrontDomain target = ((FrontDropdown)this.yName).getTarget();
			Translate tsl = new Translate(target);
			StatementList tsls = tsl.generateControllerMethod().getMethodStatementList();
			tsls.setSerial(79000L);
			tsls.setIndent(2);
			sList.add(tsls);
		}
		StatementList listoptionsl = this.listOption.generateControllerMethod().getMethodStatementList();
		listoptionsl.setSerial(89000L);
		listoptionsl.setIndent(2);
		sList.add(listoptionsl);
		
		StatementList listsumoptionsl = this.listSumOption.generateControllerMethod().getMethodStatementList();
		listsumoptionsl.setSerial(115000L);
		listsumoptionsl.setIndent(2);
		sList.add(listsumoptionsl);
		sList.add(new Statement(149000L,1,"}"));
		sList.add(new Statement(150000L,0,"}"));

		return WriteableUtil.merge(sList);
	}

	@Override
	public boolean parse() {
		// TODO Auto-generated method stub
		return false;
	}

	public FrontDomain getDomain() {
		return domain;
	}

	public void setDomain(FrontDomain domain) {
		this.domain = domain;
	}

	public String getDetailPrefix() {
		return detailPrefix;
	}

	public void setDetailPrefix(String detailPrefix) {
		this.detailPrefix = detailPrefix;
	}

	public List<FrontField> getxAxisFields() {
		return xAxisFields;
	}

	public void setxAxisFields(List<FrontField> xAxisFields) {
		this.xAxisFields = xAxisFields;
	}

	public FrontField getyName() {
		return yName;
	}

	public void setyName(FrontField yName) {
		this.yName = yName;
	}

	public Set<FrontDomain> getTranlateDomains() {
		return tranlateDomains;
	}

	public void setTranlateDomains(Set<FrontDomain> tranlateDomains) {
		this.tranlateDomains = tranlateDomains;
	}

	public Set<FrontDomain> getParentTranlateDomains() {
		return parentTranlateDomains;
	}

	public void setParentTranlateDomains(Set<FrontDomain> parentTranlateDomains) {
		this.parentTranlateDomains = parentTranlateDomains;
	}

	public Setup getSetup() {
		return setup;
	}

	public void setSetup(Setup setup) {
		this.setup = setup;
	}

	public void setListOption(ListActiveOption listOption) {
		this.listOption = listOption;
	}

	public void setListSumOption(ListActiveSumOption listSumOption) {
		this.listSumOption = listSumOption;
	}

	public ListActiveOption getListOption() {
		return listOption;
	}

	public ListActiveSumOption getListSumOption() {
		return listSumOption;
	}

}
